#!/bin/bash

VERSION=0.5.0
version()
{
    echo "rmm version: V"$VERSION
}

TRASHPATH=$HOME/.trash
checkTrash()
{
    if [ ! -d "$TRASHPATH" ];then
        mkdir $TRASHPATH/
    fi
}

DATEPATH=`date +%Y%m%d`
checkDate()
{
    if [ ! -d "$TRASHPATH/$DATEPATH/" ];then
        mkdir $TRASHPATH/$DATEPATH/
        touch $TRASHPATH/$DATEPATH/removelog
        touch $TRASHPATH/$DATEPATH/restorelog
    fi
}

checkExist()
{
    EXIST=0
    if [ -d "$1" ];then
        EXIST=1
    fi
    if [ -f "$1" ];then
        EXIST=1
    fi
    echo $EXIST
    return $?
}

checkSize()
{
    local empty=$(ls $TRASHPATH|wc -l)
    if [ $empty != "0" ]; then
        size="`du -s $TRASHPATH`"
        for i in $size
        do
            size=$i
            break
        done
        if [ $size -gt 1048576 ]; then
            size="`du -s -h $TRASHPATH`"
            for i in $size
            do
                size=$i
                break
            done
            echo Trash is too large: $size
        fi
    fi
}

EXIST=0
showTrashDate()
{
    EXIST=0
    local empty=$(ls $TRASHPATH|wc -l)
    if [ $empty != "0" ]; then
        for dir in $TRASHPATH/*
        do
            if [ -d "$dir" ]; then
                BASENAME=$(basename $dir)
                size="`du -s -h $dir`"
                for i in $size
                do
                    size=$i
                    break
                done
                echo $BASENAME "-->" $size
                EXIST=1
            fi
        done
        size="`du -s -h $TRASHPATH`"
        for i in $size
        do
            size=$i
            break
        done
        echo 'Total    -->' $size
    fi
    checkSize
}
checkDeleteTrash()
{
    EXIST=0
    dir=$TRASHPATH/$1
    if [ -d "$dir" ];then
        EXIST=1
    fi
    if [ -f "$dir" ];then
        EXIST=1
    fi
    if [ -z "$1" ];then
        EXIST=0
    fi
    echo $EXIST
    return $?
}
deleteTrashByDate()
{
    if [ "$3" == "" ]; then
        showTrashDate
        if [ $EXIST -eq 0 ]; then
            return
        fi
        echo -n "delete: "
        read dir
    else
        echo "delete: $3"
        dir=$3
    fi
    if [ "$dir" != "" ]; then
        local exist=$(checkDeleteTrash $dir)
        if [ -n $exist ]; then
            /bin/rm -fr $TRASHPATH/$dir
        fi
    fi
}
deleteTrashAll()
{
    # checkSize
    local empty=$(ls $TRASHPATH|wc -l)
    if [ $empty != "0" ]; then
        echo -n "delete all? [y/n]: "
        read order
        if [ "$order" = "y" ]; then
            # /bin/rm -fr $TRASHPATH/*
            empty_dir=$TRASHPATH/blank
            if [ ! -d $empty_dir ]; then
                mkdir $empty_dir
            fi
            echo "clearing..."
            if [ "$1" == "f" ]; then
                sudo rsync --delete-before -d $empty_dir/ $TRASHPATH/
            else
                rsync --delete-before -d $empty_dir/ $TRASHPATH/
            fi
            echo "clear done."
        fi
    fi
}

SRC=""
DST=""
EMPTY=1
showBack()
{
    EMPTY=1
    for dir in $TRASHPATH/*
    do
        if [ -d "$dir" ]; then
            BASENAME=$(basename $dir)
            if [ -n "$(cat $dir/removelog)" ];
            then
                if [ -z "$1" ];then
                    echo $BASENAME":"
                fi
                EMPTY=0
            fi
            while read line
            do
                src=`echo $line|awk -F ' ' '{print $1}'`
                dst=`echo $line|awk -F ' ' '{print $2}'`
                if [ -n "$1" ];then
                    flag=$(echo $BASENAME/$(basename $dst) | awk "/$(basename $1)/")
                    if [ -n "$flag" ];then
                        sed -i "/$(basename $dst)/d" $dir/removelog
                        echo "Restore $BASENAME/$(basename $dst) --> $src"
                        echo "Restore $BASENAME/$(basename $dst) --> $src" >> $TRASHPATH/$DATEPATH/restorelog
                        mv -f $dst $src
                    fi
                else
                    echo "    --  $src --> $BASENAME/$(basename $dst)"
                fi
            done < $dir/removelog
        fi
    done
}
back()
{
    showBack
    if [ $EMPTY -eq 1 ];then
        return
    fi
    echo -n "restore: "
    read dir
    if [ "$dir" != "" ]; then
        exist=$(checkDeleteTrash $dir)
        if [ $exist -eq 0 ];then
            echo "Invalid $dir"
            return
        fi
        showBack $dir
    fi
}
install()
{
    echo export PATH=$PATH:$(pwd) >> ~/.bashrc
    . ~/.bashrc
    chmod a+x $(pwd)/rmm
}


help()
{
    echo '--rmm: help information'
    echo '       --version    get rmm version'
    echo '       -h --help    get help information'
    echo '       -s --show    show trash bt date'
    echo '       -l --list    list trash bt date'
    echo '       -b --back    restore the delete files[dirs]'
    echo '       -d --delete  delete by date'
    echo '       -D           delete all'
    echo '       -f           delete all by root permission'
}

checkTrash

TEMP=$(getopt -q -o v,s,l,f,i,I,r,R,d,v,b,h,D --long help,show,list,force,version,delete,back -- "$@")
if [ "$?" != "0" ]; then
    echo '--rmm: Invalid option' $@
    help
    exit 1
fi
eval set -- "$TEMP"

while true; do
    case "$1" in
        -h|--help)
            help
            exit 1;;
        -i|--install)
            install
            exit 1;;
        -v|--version)
            version
            exit 1;;
        -s|--show)
            showTrashDate
            shift 1;;
        -l|--list)
            showTrashDate
            shift 1;;
        -d|--delete)
            deleteTrashByDate $@
            shift 1;;
        -f|--force)
            deleteTrashAll "f"
            shift 1;;
        -D)
            deleteTrashAll
            shift 1;;
        -b|--back)
            back
            shift 1;;
        "--") shift; break;;
        *)
            shift;;
    esac
done

for arg in $@; do
    EXIST=$(checkExist $arg)
    if [ $EXIST -eq 1 ]; then
        checkDate
        head="$(date "+%H_%M_%S_%N")"_
        BASENAME=$(basename $arg)
        src=$(readlink -f $arg)
        dst=$TRASHPATH/$DATEPATH/$head$BASENAME
        if [ -h "$arg" ] # soft link file
        then
            rm -fr "$arg"
        else
            echo "$src $dst" >> $TRASHPATH/$DATEPATH/removelog
            mv -f "$src" "$dst"
        fi
    fi
done